{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ed52592-1259-4671-b968-9bc41595b879",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright (c) OpenMMLab. All rights reserved.\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "import sys\n",
    "\n",
    "import asyncio\n",
    "from argparse import ArgumentParser\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5915161f-764c-446d-b1d7-eaa2c3e9e2ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext memory_profiler\n",
    "from mmdet.apis import (async_inference_detector, inference_detector,\n",
    "                        init_detector, show_result_pyplot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c31e89e5-fc73-4c3a-9447-4d65aaf19dac",
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_args():\n",
    "    parser = ArgumentParser()\n",
    "    parser.add_argument('imgList', help='Image file')\n",
    "    parser.add_argument('config', help='Config file')\n",
    "    parser.add_argument('checkpoint', help='Checkpoint file')\n",
    "    parser.add_argument(\n",
    "        '--device', default='cuda:0', help='Device used for inference')\n",
    "    parser.add_argument(\n",
    "        '--score-thr', type=float, default=0.3, help='bbox score threshold')\n",
    "    parser.add_argument(\n",
    "        '--async-test',\n",
    "        action='store_true',\n",
    "        help='whether to set async options for async inference.')\n",
    "    args = parser.parse_args()\n",
    "    return args\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "72af3f38-a67f-4981-bdab-9b5895a82659",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main(args):\n",
    "    output_dir = \"/home/musk/video_bak/frames/frames/output\"\n",
    "    # build the model from a config file and a checkpoint file\n",
    "    model = init_detector(args[\"config\"], args[\"checkpoint\"], device=args[\"device\"])\n",
    "    \n",
    "    i = 0\n",
    "    \n",
    "    # test a list image\n",
    "    with open(args[\"imgList\"], 'r') as fr:\n",
    "        imgList = fr.readlines()\n",
    "\n",
    "        for img_path in tqdm(imgList):\n",
    "            \n",
    "            i = i + 1\n",
    "            if i > 10:\n",
    "                return 0\n",
    "\n",
    "            name = os.path.basename(img_path).split(\".jpg\")[0]\n",
    "            out_file = os.path.join(output_dir, name + \".jpg\")\n",
    "\n",
    "            result = inference_detector(model, img_path.strip())\n",
    "            # result = inference_detector(model, img_path.strip())\n",
    "\n",
    "            model.show_result(img_path.strip(),\n",
    "                    result,\n",
    "                    score_thr=0.3,\n",
    "                    bbox_color=(72, 101, 241),\n",
    "                    text_color=(72, 101, 241),\n",
    "                    mask_color=None,\n",
    "                    thickness=2,\n",
    "                    font_size=13,\n",
    "                    win_name='',\n",
    "                    show=False,\n",
    "                    wait_time=0,\n",
    "                    out_file=out_file)\n",
    "\n",
    "            # show the results\n",
    "            # show_result_pyplot(model, img_path.strip(), result, score_thr=args.score_thr)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "40d6e4a0-7db8-48dc-9069-6b4a36a95096",
   "metadata": {},
   "outputs": [],
   "source": [
    "args={\n",
    "    \"imgList\":\"/home/musk/video_bak/frames/frames/i.txt\",\n",
    "    \"config\": \"configs/faster_rcnn/faster_rcnn_r101_fpn_mstrain_3x_coco.py\",\n",
    "    \"checkpoint\": \"checkpoints/faster_rcnn_r101_fpn_mstrain_3x_coco_20210524_110822-4d4d2ca8.pth\",\n",
    "    \"device\": \"cuda:0\",\n",
    "    \"score-thr\": \"0.3\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9b4e2709-6322-4bd9-a573-ce6dc4abba78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ERROR: Could not find file /tmp/ipykernel_13990/2526290299.py\n",
      "load checkpoint from local path: checkpoints/faster_rcnn_r101_fpn_mstrain_3x_coco_20210524_110822-4d4d2ca8.pth\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                                                                                                          | 0/9105 [00:00<?, ?it/s]/home/musk/yolo_ly/mmdetection/mmdet/datasets/utils.py:69: UserWarning: \"ImageToTensor\" pipeline is replaced by \"DefaultFormatBundle\" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.\n",
      "  'data pipeline in your config file.', UserWarning)\n",
      "  0%|▏                                                                                                                              | 10/9105 [00:06<1:33:18,  1.62it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "#### Profiling the function using line_profiler\n",
    "%mprun -f main main(args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48067f3b-008e-4a5e-b612-b71b62080d37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c052f4dd-a649-49d8-a9ee-970160523542",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "498b6670-b3b7-42d0-95ef-1f79506e80e2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8fe7f6d7-b696-4414-ad0f-2a3f1d5a7e0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext memory_profiler\n",
    "import numpy as np\n",
    "\n",
    "def func(x):\n",
    "    y = np.cos(x)\n",
    "    z = np.sin(x)\n",
    "    return y+z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a32ae4c1-4d17-484d-b3f9-8188c33038c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ERROR: Could not find file /tmp/ipykernel_13241/2050157169.py\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%mprun -f func func(np.linspace(0, 1, 1001))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fd58d29-89a6-42ff-b5a9-9d6a1554fec7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91a26272-25ef-4e7a-b290-7e17dfd45329",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4842c2dc-b535-4023-9949-3706cd45aedf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6bd99506-7a12-42e3-a3de-08f3e5bdfbc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#### Function Definition (Saved as example.py file)\n",
    "%load_ext memory_profiler\n",
    "def my_func(): \n",
    "    a = [1] * (10 ** 6) \n",
    "    b = [2] * (2 * 10 ** 7) \n",
    "    del b \n",
    "    return a#### Loading the line profiler within ipython/jupyter environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d0cc47c2-b095-4381-b259-05b96df7810c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ERROR: Could not find file /tmp/ipykernel_13337/4052024960.py\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%mprun -f my_func my_func()#### Memory Profile Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd862bfb-10f7-43c8-a893-2bb45c8fc841",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdf30fe1-54e1-4145-8112-2723610fd401",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0d4eb72-a476-43eb-ac98-277e66959115",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cfe3ae2-ac29-49d7-b120-9741a3a28566",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
